<?php

function _get_related_node_by_type($type = 'all') {
    $node = node_load(arg(1));
    $related_terms = array();
    $vocab_ids = array(1);
    //term related contents
    foreach ($node->taxonomy as $term) {
        if (in_array($term->vid, $vocab_ids)) {
            $related_terms[] = $term->tid;
        }
    }
    $sql_arg = array(0 => implode(',', $related_terms), 1 => arg(1));
    $sql_where = "WHERE tn.tid IN ( %s ) AND n.status = 1 AND ( img.delta = 0 or img.delta is NULL) AND n.nid != %d ";
    if ($type != 'all') {
        $sql_where .= "AND n.type = '%s' ";
        $sql_arg[] = $type;
    }
    $sql = "SELECT DISTINCT(n.nid), n.sticky, n.title, n.created, img.field_image_cache_fid as fid FROM {node} n "
            . "LEFT JOIN {content_field_image_cache} img ON n.nid = img.nid "
            . "INNER JOIN {term_node} tn ON n.vid = tn.vid " . $sql_where
            . "ORDER BY n.sticky DESC, n.created DESC limit 0, 5";

    $sql = db_rewrite_sql($sql);
    $rs = db_query($sql, $sql_arg);
    //db_queryd($sql, $sql_arg);
    $related_node = array();
    while ($row = db_fetch_object($rs)) {
        $related_node[] = $row;
    }

    return $related_node;
}
?>

